package org.nowcoder.leetcode.hashTable;

/**
 * Title  : 383. Ransom Note
 * Source : https://leetcode.cn/problems/ransom-note/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-07-10
 */
public class LeetCode_383 {
    public boolean canConstruct(String ransomNote, String magazine) {
        // int[] magazines = new int[26];
        // for (int i = 0; i < magazine.length(); i++) {
        //     char c = magazine.charAt(i);
        //     magazines[c - 'a'] += 1;
        // }
        // int[] ransomNotes = new int[26];
        // for (int i = 0; i < ransomNote.length(); i++) {
        //     char c = ransomNote.charAt(i);
        //     ransomNotes[c - 'a'] += 1;
        // }
        // for (int i = 0; i < 26; i++) {
        //     if (ransomNotes[i] > magazines[i]) {
        //         return false;
        //     }
        // }
        // return true;

        if (ransomNote.length() > magazine.length()) {
            return false;
        }
        int[] cnt = new int[26];
        for (char c : magazine.toCharArray()) {
            cnt[c - 'a']++;
        }
        for (char c : ransomNote.toCharArray()) {
            cnt[c - 'a']--;
            if (cnt[c - 'a'] < 0) {
                return false;
            }
        }
        return true;
    }
}
